package logic

import (
	"context"
	"errors"
	"zero-cloud-disk/api/models"

	"zero-cloud-disk/api/internal/svc"
	"zero-cloud-disk/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserDetailLogic {
	return &UserDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserDetailLogic) UserDetail(req *types.UserDetailReq) (resp *types.UserDetailResp, err error) {
	// 查找用户是否存在
	user := new(models.UserBasic)
	has, err := l.svcCtx.Engine.Where("identity = ?", req.Identity).
		Get(user)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, errors.New("用户不存在")
	}
	return &types.UserDetailResp{
		Name:  user.Name,
		Email: user.Email,
	}, nil
}
